<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
$shop=$this->plugin_setting('shop');
$return['shopset']=$shop;
$keywords=$this->plugin_setting('search');
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['id']);
	$verifycode=$_RQ['verifycode'];
	if (empty($verifycode)) {
		exi('请输入取货码!','error','jy_weishop/verify.selfindex/');
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$order_verify=$this->pdo_get('order_self',array('number'=>$verifycode));
	if (empty($order_verify)) 
	{
		exi('取货码错误,无权核销','error','jy_weishop/verify.selfindex/');   
	}
	if($orderid){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$uniacid,'id'=>$orderid));
	}else{
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$uniacid,'id'=>$order_verify['orderid']));
	}
	//核销店员身份
	$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if($order['storeid']){
		$prams['storeid']=$order['storeid'];
	}
	if($order['merchid']){
		$merch=model('jy_merch/verify')->checkuser($_SESSION['uid'],$order['merchid'],'',false);
		if(!$merch['id'] && $order['peisong_id'] != $_SESSION['uid']) {
			if($order['is_daifa']){
				$is_check=1;
			}else{
				exi('该商品属于商户，您不是该商户的核销员/配送员,无权核销!','error','jy_merch/verify.selfindex/'); 
			}		
		}
	}
	if(!$order['merchid'] || $is_check){
		$store=pdo_get('jy_weishop_store',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$saler=pdo_get('jy_weishop_store_worker',$prams);
		if(empty($saler) && $order['peisong_id'] != $_SESSION['uid'] && !$store){
			exi('您不是该店核销员/配送员,无权核销','error','jy_weishop/verify.selfindex/');   
		}
	}
	if (empty($order)) {
			exi('订单不存在!','error','jy_weishop/verify.selfindex/');
	}
	if($order['status']==0 || $order['status']==4){
		exi('订单未付款或已关闭!','error','jy_weishop/verify.selfindex/');
	}
	if($order['status']==4){
		exi('订单已关闭，不能核销！','error','jy_weishop/verify.selfindex/');
	}elseif($order['status']==5){
		exi('订单已申请退款，不能核销！','error','jy_weishop/verify.selfindex/');
	}elseif($order['status']==5){
		exi('订单已退款，不能核销！','error','jy_weishop/verify.selfindex/');
	}
	if ($order_verify['status'] ==1) {	
		exi('该取货码已被使用!','error','jy_weishop/verify.selfindex/');		
	}
	$order_goods=$this->pdo_getall('order_goods',array('orderid'=>$order['id']));
	foreach($order_goods as &$item){
		$goods=$this->pdo_get('goods',array('id'=>$item['goodsid']),array('title','image'));
		//商品规格
		if($item['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$item['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goods['selespename']=implode("，",$specs);
		}
		$item=array_merge($goods,$item);
	}
	$return['post']=array(
		'orderid'=>$order['id'],
		'verify_code'=>$verifycode,
	);
	$return['order']=$order;
	$return['order_goods']=$order_goods;
	set_html('title','核销');
    exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$verify_code=trim($post['verify_code']);
	$this->model('verify')->self_verify($verify_code,$_SESSION['uid']);
	exi('取货成功！','','jy_weishop/verify.selfindex/');
	
}